package com.wanbatv.wanba.ahitv.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.wanbatv.wanba.ahitv.Fittimeutil.FittimeErjiDemo;
import com.wanbatv.wanba.ahitv.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class FittimeErjiActivity extends Activity {
    private TextView fit_main_erji_time;
    private static final int msgKey1 = 1;
    private ViewPager viewpager;
    ArrayList<View> viewContainter = new ArrayList<View>();
    private RelativeLayout fit_erji_bg;
    private int YEMA_NUM=1;
    private ImageView fit_daka_erji_iv;
    private TextView fittime_erji_yema;
    private Map<String,Bitmap> map=new HashMap<String,Bitmap>();
//    FittimeErjiDemo d1;
//    FittimeErjiDemo d2;
//    FittimeErjiDemo d3;
    public static int arr_num=0;
    public static String vedio=null;
    String vedio_url1=null;
    String vedio_url2=null;
    String vedio_url3=null;
    public static String vedio_name=null;
    String vedio_name1=null;
    String vedio_name2=null;
    String vedio_name3=null;

    public static String content_code=null;
    String content_code1=null;
    String content_code2=null;
    String content_code3=null;
    public static String cate_code=null;
    String cate_code1=null;
    String cate_code2=null;
    String cate_code3=null;
    public static String day=null;
    String day1=null;
    String day2=null;
    String day3=null;
    JSONArray arr_url;

    private FittimeErjiDownAdapter mAdapter = null;
    String url_renshu=null;
    boolean[] shuzu=new boolean[50];
    public  static boolean dinggou=false;
    public  static boolean dinggou2=false;

    private AlertDialog myDialog=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fittime_erji);
        for (int i=0;i<50;i++){
            shuzu[i]=false;
        }
        fit_erji_bg=(RelativeLayout)findViewById(R.id.fit_erji_bg);
        if(FittimeActivity.WHICH_NUM==1){
            fit_erji_bg.setBackgroundResource(R.drawable.fittime_erji_majiaxian_bg);
        }else if(FittimeActivity.WHICH_NUM==2){
            fit_erji_bg.setBackgroundResource(R.drawable.fittime_erji_bailingnan_bg);
        }
        else if(FittimeActivity.WHICH_NUM==3){
            fit_erji_bg.setBackgroundResource(R.drawable.fittime_erji_wuyang_bg);
        }
        else if(FittimeActivity.WHICH_NUM==4){
            fit_erji_bg.setBackgroundResource(R.drawable.fittime_erji_nengliang_bg);
        }
        fittime_erji_yema=(TextView)findViewById(R.id.fittime_erji_yema);
        fit_main_erji_time=(TextView)findViewById(R.id.fit_main_erji_time);
        new TimeThread().start();
        mAdapter = new FittimeErjiDownAdapter();
        viewpager=(ViewPager)findViewById(R.id.fit_viewpager);
        viewpager.setAdapter(mAdapter);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(final int position) {
                    seturl(arr_url, position * 3);
                if(position==viewpager.getCurrentItem()) {
                    if (YEMA_NUM < position) {
                        View a = viewContainter.get(position);
                        FittimeErjiDemo b=(FittimeErjiDemo)a.findViewById(R.id.fittime_relayout_demo1);
                       ImageView iv= (ImageView)b.findViewById(R.id.fittime_guangbiao_erji_guangbiao);
                       iv.requestFocus();
                        YEMA_NUM = position;
                    }
                    if (YEMA_NUM > position) {
                        View a = viewContainter.get(position);
                        FittimeErjiDemo b=(FittimeErjiDemo)a.findViewById(R.id.fittime_relayout_demo3);
                        ImageView iv= (ImageView)b.findViewById(R.id.fittime_guangbiao_erji_guangbiao);
                        iv.requestFocus();
                        YEMA_NUM = position;
                    }
                }
                fittime_erji_yema.setText(position+1+"/"+viewContainter.size());
                if(shuzu[position]==false){
                    if(position>0){
                        View v1=viewContainter.get(position);
                        FittimeErjiDemo d1=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo1);
                        FittimeErjiDemo d2=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo2);
                        FittimeErjiDemo d3=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo3);
                        final ImageView imageView1=(ImageView)d1.findViewById(R.id.fit_erji_ditu);
                        final ImageView imageView2=(ImageView)d2.findViewById(R.id.fit_erji_ditu);
                        final ImageView imageView3=(ImageView)d3.findViewById(R.id.fit_erji_ditu);
                        try {
                            if(d1.getVisibility()!=View.GONE) {
                                JSONObject a1 = arr_url.getJSONObject(position * 3);
                                String url_iv1 = a1.getString("cover");
                                RequestQueue mQueue = Volley.newRequestQueue(FittimeErjiActivity.this);
                                ImageRequest imageRequest = new ImageRequest(
                                        url_iv1,
                                        new Response.Listener<Bitmap>() {
                                            @Override
                                            public void onResponse(Bitmap response) {
                                                imageView1.setImageBitmap(response);
                                                map.put(position * 3 + "num1", response);
                                            }
                                        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                    }
                                });
                                mQueue.add(imageRequest);
                            }
                            if(d2.getVisibility()!=View.GONE) {
                                JSONObject a2 = arr_url.getJSONObject(position * 3 + 1);
                                String url_iv2 = a2.getString("cover");
                                RequestQueue mQueue2 = Volley.newRequestQueue(FittimeErjiActivity.this);
                                ImageRequest imageRequest2 = new ImageRequest(
                                        url_iv2,
                                        new Response.Listener<Bitmap>() {
                                            @Override
                                            public void onResponse(Bitmap response) {
                                                imageView2.setImageBitmap(response);
                                                map.put(position * 3 + "num2", response);
                                            }
                                        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                    }
                                });
                                mQueue2.add(imageRequest2);
                            }
                            if(d3.getVisibility()!=View.GONE) {
                                JSONObject a3 = arr_url.getJSONObject(position * 3 + 2);
                                String url_iv3 = a3.getString("cover");
                                RequestQueue mQueue3 = Volley.newRequestQueue(FittimeErjiActivity.this);
                                ImageRequest imageRequest3 = new ImageRequest(
                                        url_iv3,
                                        new Response.Listener<Bitmap>() {
                                            @Override
                                            public void onResponse(Bitmap response) {
                                                imageView3.setImageBitmap(response);
                                                map.put(position * 3 + "num3", response);
                                            }
                                        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                    }
                                });
                                mQueue3.add(imageRequest3);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
                shuzu[position]=true;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        String url=null;
        if(FittimeActivity.WHICH_NUM==1){
        url="http://61.191.46.214:8080/wanba_shzg/ott_channel_fittime_cate_001.jsp";
        }else if(FittimeActivity.WHICH_NUM==2){
            url="http://61.191.46.214:8080/wanba_shzg/ott_channel_fittime_cate_002.jsp";
        }
        else if(FittimeActivity.WHICH_NUM==3){
            url="http://61.191.46.214:8080/wanba_shzg/ott_channel_fittime_cate_003.jsp";
        }else if(FittimeActivity.WHICH_NUM==4){
            url="http://61.191.46.214:8080/wanba_shzg/ott_channel_fittime_cate_004.jsp";
        }

        if(FittimeActivity.WHICH_NUM==1){
            url_renshu="http://61.191.46.214:8087/fittime/usercount?category=001";
        }else if(FittimeActivity.WHICH_NUM==2){
            url_renshu="http://61.191.46.214:8087/fittime/usercount?category=002";
        }
        else if(FittimeActivity.WHICH_NUM==3){
            url_renshu="http://61.191.46.214:8087/fittime/usercount?category=003";
        }else if(FittimeActivity.WHICH_NUM==4){
            url_renshu="http://61.191.46.214:8087/fittime/usercount?category=004";
        }

        RequestQueue rq = Volley.newRequestQueue(this.getApplicationContext());
        JsonObjectRequest request = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {
                            arr_num=0;
                            YEMA_NUM=0;
                            JSONArray arr=jsonObject.getJSONArray("result");
                            arr_url=arr;
                            seturl(arr_url, 0);

                            int list=arr.length()/3;
                            if(arr.length()%3!=0){
                                list+=1;
                            }
                            for(int i=0;i<list;i++){
                                View v1 = LayoutInflater.from(FittimeErjiActivity.this).inflate(R.layout.fittime_erji_relayout, null);
                                demo_demo_change(v1);

                                setDate(arr, arr_num, v1);
                                setrenshu(arr, arr_num,v1);
                                viewContainter.add(v1);
                                FittimeErjiDemo d1=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo1);
                                FittimeErjiDemo d2=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo2);
                                FittimeErjiDemo d3=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo3);
                                ImageView iv1=(ImageView)d1.findViewById(R.id.fittime_guangbiao_erji_guangbiao);
                                ImageView iv2=(ImageView)d2.findViewById(R.id.fittime_guangbiao_erji_guangbiao);
                                ImageView iv3=(ImageView)d3.findViewById(R.id.fittime_guangbiao_erji_guangbiao);

                                iv1.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (vedio_url1 != null) {
                                            vedio = vedio_url1;
                                        }
                                        if(vedio_name1!=null){
                                            vedio_name=vedio_name1;
                                        }
                                        if(content_code1!=null){
                                            content_code=content_code1;
                                        }
                                        if(cate_code1!=null){
                                            cate_code=cate_code1;
                                        }
                                        if(day1!=null){
                                            day=day1;
                                        }

                                        if(LoginActivity.is_dinggou){
                                            Intent it = new Intent();
                                            it.setClass(FittimeErjiActivity.this, FittimeSimplePlayerActivity.class);
                                            startActivity(it);
                                        }else {
//                                            dinggou=true;
//                                            Intent it = new Intent();
//                                            it.setClass(FittimeErjiActivity.this, PayActivity.class);
//                                            startActivity(it);
                                            creatAlertDialog_tuichu();
                                        }

                                    }
                                });
                                iv2.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (vedio_url2 != null) {
                                            vedio = vedio_url2;
                                        }
                                        if(vedio_name2!=null){
                                            vedio_name=vedio_name2;
                                        }
                                        if(content_code2!=null){
                                            content_code=content_code2;
                                        }
                                        if(cate_code2!=null){
                                            cate_code=cate_code2;
                                        }
                                        if(day2!=null){
                                            day=day2;
                                        }
                                        if(LoginActivity.is_dinggou){
                                            Intent it = new Intent();
                                            it.setClass(FittimeErjiActivity.this, FittimeSimplePlayerActivity.class);
                                            startActivity(it);
                                        }else {
//                                            dinggou=true;
//                                            Intent it = new Intent();
//                                            it.setClass(FittimeErjiActivity.this, PayActivity.class);
//                                            startActivity(it);
                                            creatAlertDialog_tuichu();
                                        }
                                    }
                                });
                                iv3.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (vedio_url3 != null) {
                                            vedio = vedio_url3;
                                        }
                                        if(vedio_name3!=null){
                                            vedio_name=vedio_name3;
                                        }
                                        if(content_code3!=null){
                                            content_code=content_code3;
                                        }
                                        if(cate_code3!=null){
                                            cate_code=cate_code3;
                                        }
                                        if(day3!=null){
                                            day=day3;
                                        }
                                        if(LoginActivity.is_dinggou){
                                            Intent it = new Intent();
                                            it.setClass(FittimeErjiActivity.this, FittimeSimplePlayerActivity.class);
                                            startActivity(it);
                                        }else {
//                                            dinggou=true;
//                                            Intent it = new Intent();
//                                            it.setClass(FittimeErjiActivity.this, PayActivity.class);
//                                            startActivity(it);
                                            creatAlertDialog_tuichu();
                                        }
                                    }
                                });
                                arr_num+=3;
                            }
                            View v1=viewContainter.get(0);

                            FittimeErjiDemo d1=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo1);
                            FittimeErjiDemo d2=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo2);
                            FittimeErjiDemo d3=(FittimeErjiDemo)v1.findViewById(R.id.fittime_relayout_demo3);
                            ImageView a=(ImageView)d1.findViewById(R.id.fittime_guangbiao_erji_guangbiao);
                            a.requestFocus();
                            final ImageView imageView1=(ImageView)d1.findViewById(R.id.fit_erji_ditu);
                            final ImageView imageView2=(ImageView)d2.findViewById(R.id.fit_erji_ditu);
                            final ImageView imageView3=(ImageView)d3.findViewById(R.id.fit_erji_ditu);
                            try {
                                if(d1.getVisibility()!=View.GONE) {
                                    JSONObject a1 = arr_url.getJSONObject(0);
                                    String url_iv1 = a1.getString("cover");
                                    RequestQueue mQueue = Volley.newRequestQueue(FittimeErjiActivity.this);
                                    ImageRequest imageRequest = new ImageRequest(
                                            url_iv1,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    imageView1.setImageBitmap(response);
                                                    map.put(0 + "num1", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue.add(imageRequest);
                                }
                                if(d2.getVisibility()!=View.GONE) {
                                    JSONObject a2 = arr_url.getJSONObject( 1);
                                    String url_iv2 = a2.getString("cover");
                                    RequestQueue mQueue2 = Volley.newRequestQueue(FittimeErjiActivity.this);
                                    ImageRequest imageRequest2 = new ImageRequest(
                                            url_iv2,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    imageView2.setImageBitmap(response);
                                                    map.put(0 + "num2", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue2.add(imageRequest2);
                                }
                                if(d3.getVisibility()!=View.GONE) {
                                    JSONObject a3 = arr_url.getJSONObject( 2);
                                    String url_iv3 = a3.getString("cover");
                                    RequestQueue mQueue3 = Volley.newRequestQueue(FittimeErjiActivity.this);
                                    ImageRequest imageRequest3 = new ImageRequest(
                                            url_iv3,
                                            new Response.Listener<Bitmap>() {
                                                @Override
                                                public void onResponse(Bitmap response) {
                                                    imageView3.setImageBitmap(response);
                                                    map.put( 0+ "num3", response);
                                                }
                                            }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
                                        @Override
                                        public void onErrorResponse(VolleyError error) {
//                                    erweima_login.setImageResource(R.drawable.default_image);
                                        }
                                    });
                                    mQueue3.add(imageRequest3);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            fittime_erji_yema.setText("1/"+viewContainter.size());
                            mAdapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }

        );
        rq.add(request);

        fit_daka_erji_iv=(ImageView)findViewById(R.id.fit_daka_erji_iv);
        fit_daka_erji_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent();
                it.setClass(FittimeErjiActivity.this, FittimeFengxiangActivity.class);
                startActivity(it);
            }
        });

    }
    public class TimeThread extends Thread {
        @Override
        public void run () {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = msgKey1;
                    mHandler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while(true);
        }
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage (Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case msgKey1:
                    long sysTime = System.currentTimeMillis();
                    CharSequence sysTimeStr = DateFormat.format("hh:mm aa", sysTime);
                    fit_main_erji_time.setText(sysTimeStr);
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_fittime_erji, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void demo_demo_change(View v){
        FittimeErjiDemo d1=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo1);
        FittimeErjiDemo d2=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo2);
        FittimeErjiDemo d3=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo3);
        ImageView iv1=(ImageView)d1.findViewById(R.id.fit_erji_demo_iv_down);
        ImageView iv2=(ImageView)d2.findViewById(R.id.fit_erji_demo_iv_down);
        ImageView iv3=(ImageView)d3.findViewById(R.id.fit_erji_demo_iv_down);

        if(FittimeActivity.WHICH_NUM==1){
            iv1.setImageResource(R.drawable.fittime_down_1);
            iv2.setImageResource(R.drawable.fittime_down_1);
            iv3.setImageResource(R.drawable.fittime_down_1);
        }else if(FittimeActivity.WHICH_NUM==2){
            iv1.setImageResource(R.drawable.fittime_down_2);
            iv2.setImageResource(R.drawable.fittime_down_2);
            iv3.setImageResource(R.drawable.fittime_down_2);
        }
        else if(FittimeActivity.WHICH_NUM==3){
            iv1.setImageResource(R.drawable.fittime_down_3);
            iv2.setImageResource(R.drawable.fittime_down_3);
            iv3.setImageResource(R.drawable.fittime_down_3);
        }
        else if(FittimeActivity.WHICH_NUM==4){
            iv1.setImageResource(R.drawable.fittime_down_4);
            iv2.setImageResource(R.drawable.fittime_down_4);
            iv3.setImageResource(R.drawable.fittime_down_4);
        }

    }

    public void setDate(JSONArray arr,final int num,View v){
        try {
            FittimeErjiDemo d1=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo1);
            FittimeErjiDemo d2=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo2);
            FittimeErjiDemo d3=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo3);

            if(num<arr.length()) {
                JSONObject obj1 = arr.getJSONObject(num);

                final ImageView imageView=(ImageView)d1.findViewById(R.id.fit_erji_ditu);
                TextView tv1=(TextView)d1.findViewById(R.id.fit_erji_time);
                TextView tv2=(TextView)d1.findViewById(R.id.fit_erji_xiangmu);
                TextView tv3=(TextView)d1.findViewById(R.id.fit_erji_shuliang);
                String IV_URL=obj1.getString("cover");
                tv1.setText(obj1.getString("day"));
                tv2.setText(obj1.getString("title"));
//                RequestQueue mQueue = Volley.newRequestQueue(FittimeErjiActivity.this);
//                ImageRequest imageRequest = new ImageRequest(
//                        IV_URL,
//                        new Response.Listener<Bitmap>() {
//                            @Override
//                            public void onResponse(Bitmap response) {
//                                imageView.setImageBitmap(response);
//                                map.put(num+"num1",response);
//                            }
//                        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
////                                    erweima_login.setImageResource(R.drawable.default_image);
//                    }
//                });
//                mQueue.add(imageRequest);
            }else{
                d1.setVisibility(View.GONE);
            }
            if(num+1<arr.length()) {
                JSONObject obj2 = arr.getJSONObject(num+1);
                final ImageView imageView=(ImageView)d2.findViewById(R.id.fit_erji_ditu);
                TextView tv1=(TextView)d2.findViewById(R.id.fit_erji_time);
                TextView tv2=(TextView)d2.findViewById(R.id.fit_erji_xiangmu);
                TextView tv3=(TextView)d2.findViewById(R.id.fit_erji_shuliang);
                String IV_URL=obj2.getString("cover");
                tv1.setText(obj2.getString("day"));
                tv2.setText(obj2.getString("title"));
//                RequestQueue mQueue = Volley.newRequestQueue(FittimeErjiActivity.this);
//                ImageRequest imageRequest = new ImageRequest(
//                        IV_URL,
//                        new Response.Listener<Bitmap>() {
//                            @Override
//                            public void onResponse(Bitmap response) {
//                                imageView.setImageBitmap(response);
//                                map.put(num + "num2", response);
//                            }
//                        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
////                                    erweima_login.setImageResource(R.drawable.default_image);
//                    }
//                });
//                mQueue.add(imageRequest);
            }else{
                d2.setVisibility(View.GONE);
            }
            if(num+2<arr.length()) {
                JSONObject obj3 = arr.getJSONObject(num+2);
                final ImageView imageView=(ImageView)d3.findViewById(R.id.fit_erji_ditu);
                TextView tv1=(TextView)d3.findViewById(R.id.fit_erji_time);
                TextView tv2=(TextView)d3.findViewById(R.id.fit_erji_xiangmu);
                TextView tv3=(TextView)d3.findViewById(R.id.fit_erji_shuliang);
                String IV_URL=obj3.getString("cover");
                tv1.setText(obj3.getString("day"));
                tv2.setText(obj3.getString("title"));
//                RequestQueue mQueue = Volley.newRequestQueue(FittimeErjiActivity.this);
//                ImageRequest imageRequest = new ImageRequest(
//                        IV_URL,
//                        new Response.Listener<Bitmap>() {
//                            @Override
//                            public void onResponse(Bitmap response) {
//                                imageView.setImageBitmap(response);
//                                map.put(num+"num3",response);
//
//                            }
//                        }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
//                    @Override
//                    public void onErrorResponse(VolleyError error) {
////                                    erweima_login.setImageResource(R.drawable.default_image);
//                    }
//                });
//                mQueue.add(imageRequest);
            }else{
                d3.setVisibility(View.GONE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public void setrenshu(JSONArray arr,int num,View v){
        FittimeErjiDemo d1=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo1);
        FittimeErjiDemo d2=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo2);
        FittimeErjiDemo d3=(FittimeErjiDemo)v.findViewById(R.id.fittime_relayout_demo3);
        if(num<arr.length()){
            final TextView tv1=(TextView)d1.findViewById(R.id.fit_erji_shuliang);
            try {
                final JSONObject obj=arr.getJSONObject(num);
                RequestQueue rq1 = Volley.newRequestQueue(this.getApplicationContext());
                JsonObjectRequest request1 = new JsonObjectRequest(url_renshu, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                try {
                                    JSONObject renshu=jsonObject.getJSONObject("data");
                                    JSONArray arr_renshu=renshu.getJSONArray("list");
                                    for (int i=0;i<arr_renshu.length();i++){
                                        JSONObject a=arr_renshu.getJSONObject(i);
                                        if(obj.getString("content_code").equals(a.getString("contentCode"))){
                                            tv1.setText("当前有"+a.getString("userCount")+"人完成了此项目");
                                        }
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                }

                );
                rq1.add(request1);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        if(num+1<arr.length()){
            final TextView tv1=(TextView)d2.findViewById(R.id.fit_erji_shuliang);
            try {
                final JSONObject obj=arr.getJSONObject(num+1);
                RequestQueue rq2 = Volley.newRequestQueue(this.getApplicationContext());
                JsonObjectRequest request2 = new JsonObjectRequest(url_renshu, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                try {
                                    JSONObject renshu=jsonObject.getJSONObject("data");
                                    JSONArray arr_renshu=renshu.getJSONArray("list");
                                    for (int i=0;i<arr_renshu.length();i++){
                                        JSONObject a=arr_renshu.getJSONObject(i);
                                        if(obj.getString("content_code").equals(a.getString("contentCode"))){
                                            tv1.setText("当前有"+a.getString("userCount")+"人完成了此项目");
                                        }
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                }

                );
                rq2.add(request2);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        if(num+2<arr.length()){
            final TextView tv1=(TextView)d3.findViewById(R.id.fit_erji_shuliang);
            try {
                final JSONObject obj=arr.getJSONObject(num+2);
                RequestQueue rq3 = Volley.newRequestQueue(this.getApplicationContext());
                JsonObjectRequest request3 = new JsonObjectRequest(url_renshu, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                try {
                                    JSONObject renshu=jsonObject.getJSONObject("data");
                                    JSONArray arr_renshu=renshu.getJSONArray("list");
                                    for (int i=0;i<arr_renshu.length();i++){
                                        JSONObject a=arr_renshu.getJSONObject(i);
                                        if(obj.getString("content_code").equals(a.getString("contentCode"))){
                                            tv1.setText("当前有"+a.getString("userCount")+"人完成了此项目");
                                        }
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                    }
                }

                );
                rq3.add(request3);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }
   public void  seturl(JSONArray arr,int num){
       try {
           if(num<arr.length()) {
               JSONObject obj1 = arr.getJSONObject(num);
               vedio_url1=obj1.getString("video");
               vedio_name1=obj1.getString("title");
               content_code1=obj1.getString("content_code");
               cate_code1=obj1.getString("cate_code");
               day1=obj1.getString("day");
           }
           if(num+1<arr.length()) {
               JSONObject obj2 = arr.getJSONObject(num+1);
               vedio_url2=obj2.getString("video");
               vedio_name2=obj2.getString("title");
               content_code2=obj2.getString("content_code");
               cate_code2=obj2.getString("cate_code");
               day2=obj2.getString("day");
           }
           if(num+2<arr.length()) {
               JSONObject obj3 = arr.getJSONObject(num+2);
               vedio_url3=obj3.getString("video");
               vedio_name3=obj3.getString("title");
               content_code3=obj3.getString("content_code");
               cate_code3=obj3.getString("cate_code");
               day3=obj3.getString("day");
           }
       } catch (JSONException e) {
           e.printStackTrace();
       }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            arr_num=0;
            YEMA_NUM=0;
            finish();
        }else {
            return super.onKeyDown(keyCode, event);
        }
        return super.onKeyDown(keyCode, event);
    }

    private class FittimeErjiDownAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return viewContainter.size();
        }

        //滑动切换的时候销毁当前的组件
        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            ((ViewPager) container).removeView(viewContainter.get(position));
        }

        //每次滑动的时候生成的组件
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ((ViewPager) container).addView(viewContainter.get(position));
            return viewContainter.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public float getPageWidth(int position) {
            float num = 0.93f;
            return num;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(LoginActivity.is_dinggou){
            dinggou=false;
            dinggou2=false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Iterator iter = map.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey();
            Object val = entry.getValue();
            Bitmap a=(Bitmap)val;
            a.recycle();
        }
        System.gc();
    }
    @Override
    protected void onStart() {
        super.onStart();
        if(dinggou && dinggou2){
            dinggou=false;
            dinggou2=false;
            Intent it = new Intent();
            it.setClass(FittimeErjiActivity.this, FittimeSimplePlayerActivity.class);
            startActivity(it);
        }
    }
    private void creatAlertDialog_tuichu() {

        myDialog = new AlertDialog.Builder(FittimeErjiActivity.this).create();

        myDialog.show();

        myDialog.getWindow().setContentView(R.layout.diolag_queding);

        myDialog.getWindow()
                .findViewById(R.id.no_iv)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        myDialog.dismiss();

                    }

                });
        myDialog.getWindow()
                .findViewById(R.id.yes_iv)
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        myDialog.dismiss();
                        dinggou=true;
                        Intent it=new Intent();
                        it.setClass(FittimeErjiActivity.this,PayActivity.class);
                        startActivity(it);

                    }

                });
    }
}
